home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 891 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  2. Message-ID: <315a9640.1061096@news.ultranet.com>
  3. X-Original-Date: Thu, 28 Mar 1996 13:57:42 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 28 Mar 96 13:59:51 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Enumerated type converted to pointer? Legal?
  9. Organization: UltraNet Communications, Inc.
  10. References: <4hobji$mco@netlab.cs.rpi.edu> <KANZE.96Mar22115626@gabi.gabi-soft.fr>
  11. X-Newsreader: Forte Agent .99d/16.182
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMVqbaeEDnX0m9pzZAQHCbgF/V+IrHfuP9nqDSKCYbax/R/fk1gJnTNs2
  14.     24RHgMJW7wBQ6IvSWBo0RmboJkOavuQ2
  15.     =t2u7
  16.  
  17. kanze@gabi-soft.fr (J. Kanze) wrote:
  18.  
  19. >The error may be historically conditioned.  As I interpret the C
  20. >standard, an enumerated constant has type int, and a constant integral
  21. >expression which evalutates to 0 is a null pointer.  Even in the ARM,
  22. >however, ``An enumeration is a distinct integral type.''
  23.  
  24. That begs the question, however, whether being an integral type with
  25. constant value zero is sufficient to allow assignment to a pointer.
  26. Let me illustrate:
  27.  
  28.     void *p = 0;
  29.  
  30. This is definately legal. The pointer can be initialized or assigned
  31. from the literal.
  32.  
  33.     const int nil1 = 0;
  34.     void *p = nil1;
  35.  
  36.     const unsigned char nil2 = 0;
  37.     p = nil2;
  38.  
  39. Is this legal? I believe all of the above is legal, though IMO it
  40. shouldn't be (see below)
  41.  
  42.     enum ptrNil { nil3 };
  43.     void *p = nil3;
  44.  
  45. You are claiming that this is not legal because nil3 is of type ptrNil
  46. which is not compatable with the type of p. However, I fail to see how
  47. nil3 is different from nil1 and nil2 in this respect. All are `integral'
  48. types with value zero.
  49.  
  50. I agree that the assignment of nil3 to p *should not* be legal because
  51. nil3 is not a pointer type. But by the same logic, neither should the
  52. assignments to nil1 and nil2 be legal. IMO it would be simplest to
  53. disallow implicit conversion from any integral type to a pointer type
  54. *except* in the case of literal zero. I don't think adding this
  55. prohibition would break much code. <stdlib> defines NULL as
  56.  
  57.    #define NULL 0
  58.  
  59. so any code that uses NULL or 0 for the null pointer would still work.
  60. Perhaps to cover most of the rest of the cases we could add the rule
  61. that any constant expression with value `(void *) 0' can be converted to
  62. any pointer type without a cast. This would allow nice constructions
  63. like:
  64.  
  65.    void *const nil = 0;
  66.  
  67. In summary, I think that using a zero enumeration as a null pointer *is*
  68. currently legal, but shouldn't be.
  69.  
  70. -------------------------------------------------------------
  71. Pablo Halpern                   phalpern@truffle.ultranet.com
  72.  
  73. I am self-employed. Therefore, my opinions *do* represent 
  74. those of my employer.
  75. ---
  76. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  77. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  78. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  79. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  80. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  81.